package com.xry.match.service;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xry.common.bean.Seat;
import com.xry.common.service.SeatService;
import com.xry.match.mapper.SeatMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 86152
 * @description 针对表【seat】的数据库操作Service实现
 * @createDate 2025-09-04 19:58:40
 */
@Service
@DubboService
public class SeatServiceImpl extends ServiceImpl<SeatMapper, Seat>
        implements SeatService {
    @Override
    public Seat getSeatById(Integer id) {
        LambdaQueryWrapper<Seat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Seat::getId, id).eq(Seat::getDeleted, 0);
        return super.getOne(queryWrapper);
    }


    @Override
    public boolean updateSeatById(Seat seat) {
        return super.updateById(seat);
    }

    @Override
    public boolean deleteSeatById(Integer id) {
        Seat seat = super.getById(id);
        if (seat != null){
            seat.setDeleted(1);
            return super.updateById( seat);
        }
        return false;
    }

    @Override
    public boolean addSeat(Seat seat) {
        return super.save(seat);
    }

    @Override
    public List<Seat> getAll() {
        return super.list();
    }

    @Override
    public IPage<Seat> getPage(Seat seat, IPage<Seat> page) {
        LambdaQueryWrapper<Seat> queryWrapper = new LambdaQueryWrapper<>();

        // 只查询未删除的记录
        queryWrapper.eq(Seat::getDeleted, 0);

        // 添加查询条件
        if (seat != null) {
            // 座位区域名模糊查询
            if (StringUtils.hasText(seat.getName())) {
                queryWrapper.like(Seat::getName, seat.getName().trim());
            }

            // 比赛ID精确查询
            if (seat.getMatchId() != null && seat.getMatchId() > 0) {
                queryWrapper.eq(Seat::getMatchId, seat.getMatchId());
            }

            // 价格范围查询（假设seat中添加了priceMin和priceMax字段用于查询）
            if (seat.getPrice() != null) {
                // 这里仅作为示例，实际应根据具体需求调整
                queryWrapper.ge(Seat::getPrice, seat.getPrice());
            }

            // 座位状态精确查询
            if (seat.getStatus() != null) {
                queryWrapper.eq(Seat::getStatus, seat.getStatus());
            }

            // 剩余座位数查询（大于等于指定数量）
            if (seat.getRemaining() != null && seat.getRemaining() >= 0) {
                queryWrapper.ge(Seat::getRemaining, seat.getRemaining());
            }
        }

        // 按更新时间倒序排列（如果有更新时间字段），否则保持原逻辑
        queryWrapper.orderByDesc(Seat::getId); // 假设用ID作为默认排序

        return super.page(page, queryWrapper);
    }
}


